package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.Counts;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wjs
 * @version 1.0
 * @date 2021/12/31 0031 下午 6:48
 */

@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Counts countsByUserId(Long userId) {

        //喜欢
        Query love = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> likes = mongoTemplate.find(love, UserLike.class);
        String loveCount = String.valueOf(mongoTemplate.count(love, UserLike.class));

        //粉丝
        Query fen = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        String fenCount = String.valueOf(mongoTemplate.count(fen, UserLike.class));

        //互相喜欢
        Integer eachLoveCount = 0;
        //当前用户 喜欢用户的id
        List<Long> likeUserIds = likes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        Map<Long, List<Long>> map = likeUserIds.stream().collect(Collectors.toMap(
                likeUserId -> likeUserId,
                likeUserId -> {
                    Query query = Query.query(Criteria.where("userId").is(likeUserId).and("isLike").is(true));
                    List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
                    List<Long> collect = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
                    return collect;
                }
        ));
        for (Long likeUserId : likeUserIds) {
            List<Long> longs = map.get(likeUserId);
            if (longs.contains(userId)) {
                eachLoveCount++;
            }
        }

        return new Counts(eachLoveCount, Integer.valueOf(loveCount), Integer.valueOf(fenCount));
    }

    @Override
    public PageResult eachLove(Integer page, Integer pagesize, Long userId) {
        Query love = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> loves = mongoTemplate.find(love, UserLike.class);
        Query fen = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> fans = mongoTemplate.find(fen, UserLike.class);
        //互相喜欢
        Long eachLoveCount = 0L;
        //当前用户 喜欢用户的id
        List<Long> likeUserIds = loves.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        Map<Long, UserLike> map = fans.stream().collect(Collectors.toMap(
                userLike -> userLike.getUserId(),
                userLike -> userLike
        ));

        List<Long> eachLoves = new ArrayList<>();
        for (Long likeUserId : likeUserIds) {
            UserLike userLike = map.get(likeUserId);
            if (userLike != null && userLike.getLikeUserId() == userId) {
                eachLoveCount++;
                eachLoves.add(likeUserId);
            }
        }

        List<Long> list = eachLoves.stream().skip((page - 1) * pagesize).limit(pagesize).collect(Collectors.toList());

        Query query1 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true).and("likeUserId").in(list));
        List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);

        return new PageResult(page, pagesize, eachLoveCount, userLikes);
    }

    @Override
    public PageResult love(Integer page, Integer pagesize, Long userId) {
        Query love = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(love, UserLike.class);
        Long count = mongoTemplate.count(love, UserLike.class);
        return new PageResult(page, pagesize, count, userLikes);
    }

    @Override
    public PageResult fenLove(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageResult(page, pagesize, count, userLikes);
    }

    @Override
    public List<UserLike> loves(Long userId) {
        Query love = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        return mongoTemplate.find(love, UserLike.class);
    }

    @Override
    public Boolean find(Long userId, String uid) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(Long.valueOf(uid)).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        if(userLikes.size() == 0){
            return false;
        }
        return true;
    }

    @Override
    public void remove(Long userId, String uid) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(Long.valueOf(uid)));

        mongoTemplate.remove(query,UserLike.class);
    }

    @Override
    public void save(UserLike userLike) {
        mongoTemplate.save(userLike);
    }
}
